/*
    Dao FLTK2
    Copyright (C) 2010 Belousov Oleg <belousov.oleg@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <fltk/events.h>
#include <fltk/Widget.h>
#include <dao.h>

extern DaoVmSpace  *VmSpace;

static void widget_callback(fltk::Widget *widget, long data) {
    DaoCData    *cdata = (DaoCData*) data;
    DaoObject   *obj = DaoCData_GetObject(cdata);
    DValue      val = DaoObject_GetField(obj, "callback");

    if (val.t == DAO_ROUTINE) {
        DaoVmProcess *vmp = DaoVmSpace_AcquireProcess(VmSpace);

        DaoVmProcess_Call(vmp, val.v.routine, obj, NULL, 0);
        DaoVmSpace_ReleaseProcess(VmSpace, vmp);
    }
}

void init_callback(fltk::Widget *ptr, DaoCData *cdata) {
    ptr->callback(widget_callback);
    ptr->user_data((void*) cdata);
}

static void widget_x_set(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    int x = (int) p[1]->v.i;

    ptr->x(x);
}

static void widget_y_set(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    int y = (int) p[1]->v.i;

    ptr->y(y);
}

static void widget_w_set(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    int w = (int) p[1]->v.i;

    ptr->w(w);
}

static void widget_h_set(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    int h = (int) p[1]->v.i;

    ptr->h(h);
}

static void widget_w_get(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    DaoContext_PutInteger(ctx, (int) (ptr->w()));
}

static void widget_h_get(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    DaoContext_PutInteger(ctx, (int) (ptr->h()));
}

static void widget_r(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    DaoContext_PutInteger(ctx, (int) (ptr->x() + ptr->w()));
}

static void widget_b(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    DaoContext_PutInteger(ctx, (int) (ptr->y() + ptr->h()));
}

static void widget_redraw(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    ptr->redraw();
}

static void widget_backcolor(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    char *color = (char*) DString_GetMBS(p[1]->v.s);

    ptr->color(fltk::color(color));
}

static void widget_textcolor(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    char *color = (char*) DString_GetMBS(p[1]->v.s);

    ptr->textcolor(fltk::color(color));
}

static void widget_labelcolor(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    char *color = (char*) DString_GetMBS(p[1]->v.s);

    ptr->labelcolor(fltk::color(color));
}

static void widget_textsize(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    int size = (int) p[1]->v.i;
    ptr->textsize(size);
}

static void widget_labelsize(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    int size = (int) p[1]->v.i;
    ptr->labelsize(size);
}

static void widget_key(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    char *key = (char*) DString_GetMBS(p[1]->v.s);

    ptr->shortcut(fltk::key(key));
}

static void widget_hide(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    ptr->hide();
}

static void widget_show(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    ptr->show();
};

static void widget_size(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    int w = (int) p[1]->v.i;
    int h = (int) p[2]->v.i;

    ptr->resize(w, h);
}

static void widget_position(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);

    int x = (int) p[1]->v.i;
    int y = (int) p[2]->v.i;

    ptr->position(x, y);
}

static void widget_label(DaoContext *ctx, DValue *p[], int N) {
    fltk::Widget* ptr = (fltk::Widget*) DaoCData_GetData(p[0]->v.cdata);
    char *title= (char*) DString_GetMBS(p[1]->v.s);

    ptr->copy_label(title);
}

//

static DaoFuncItem widgetMeth[] = {
    { widget_x_set,         "x(self:fltk_widget, x:int)" },
    { widget_y_set,         "y(self:fltk_widget, y:int)" },
    { widget_w_set,         "w(self:fltk_widget, w:int)" },
    { widget_h_set,         "h(self:fltk_widget, h:int)" },

    { widget_w_get,         "w(self:fltk_widget) => int" },
    { widget_h_get,         "h(self:fltk_widget) => int" },

    { widget_r,             "r(self:fltk_widget) => int" },
    { widget_b,             "b(self:fltk_widget) => int)" },

    { widget_redraw,        "redraw(self:fltk_widget)" },
    { widget_backcolor,     "backcolor(self:fltk_widget, color:string)" },

    { widget_textcolor,     "textcolor(self:fltk_widget, color:string)" },
    { widget_textsize,      "textsize(self:fltk_widget, size:int)" },

    { widget_label,         "label(self:fltk_widget, label:string)" },
    { widget_labelcolor,    "labelcolor(self:fltk_widget, color:string)" },
    { widget_labelsize,     "labelsize(self:fltk_widget, size:int)" },

    { widget_key,           "key(self:fltk_widget, key:string)" },
    { widget_hide,          "hide(self:fltk_widget)" },
    { widget_show,          "show(self:fltk_widget)" },
    { widget_size,          "size(self:fltk_widget, w:int, h:int)" },
    { widget_position,      "position(self:fltk_widget, x:int, y:int)" },

    { NULL, NULL }
};

DaoTypeBase widgetTyper = {
    "fltk_widget",
    NULL,
    NULL,       /* no constant number */
    widgetMeth, /* methods */
    { NULL },   /* no parent type */
    { NULL },
    NULL,
    NULL        /* free by free() */
};
